#!coding:utf-8
from rest_framework.response import Response

from cvirt.manager.serializers import BackupstrategySerializer
from cvirt.manager.views import BaseListAPIView,BaseRetriveAPIView, NamedAPIViewMixIn
from cvirt.manager.models import BackupPolicy, Storage


class Backupstrategys(NamedAPIViewMixIn, BaseListAPIView):
    serializer_class = BackupstrategySerializer
    model = BackupPolicy

    def _get(self,request,*args,**kwargs):

        dataset = BackupPolicy.objects.all()
        result = self.paginate(request, dataset, BackupstrategySerializer)
        return Response(result)

    def create(self, request, *args, **kwargs):

        name = request.DATA.get("name")
        backuptype = request.DATA.get("type", None)  #0-冷备份 , 1-热备份
        compress = request.DATA.get("compress", None)
        if not compress:
            compress = False
        compresslevel = request.DATA.get("compresslevel", 0) # 0 ~ 9
        remain = request.DATA.get("remain", 0)  #0-所有 or 天数
        remaindays = request.DATA.get("remaindays", 0);
        locationtype = request.DATA.get("locationtype", None)  #0-本地 , 1-已关联存储 , 3-sftp
        localpath = request.DATA.get("localpath", None)
        storage_id = request.DATA.get("storage", None)
        sftpusername = request.DATA.get("sftpusername", None)
        sftphost = request.DATA.get("sftphost", None)
        sftppath = request.DATA.get("sftppath", None)
        sftppassword = request.DATA.get("sftppassword", None)
        backuptime = request.DATA.get("backuptime", None)
        backupfrequency = request.DATA.get("backupfrequency", None)
        weekday = request.DATA.get("weekday", None)
        monthday = request.DATA.get("monthday", None)

        backuptypevalue = None
        if backuptype:
            backuptypevalue = backuptype.get("value")

        storage = None
        if storage_id:
            storage = Storage.objects.get(id = storage_id.get("value"))

        backupfrequencyvalue = None
        if backupfrequency:
            backupfrequencyvalue = backupfrequency.get("value")

        if cmp(remain, "1") == 0:
            remain = remaindays

        weekdayvalue = None
        if weekday:
            weekdayvalue = weekday.get("name")

        monthdayvalue = None
        if monthday:
            monthdayvalue = monthday.get("value")

        backupPolity = BackupPolicy.objects.create(name=name, backuptype=backuptypevalue, compress=compress, compresslevel=compresslevel,
                                    remain=remain, locationtype=locationtype, localpath=localpath, storage=storage,
                                    sftpusername=sftpusername, sftphost=sftphost, sftppath=sftppath, sftppassword=sftppassword,
                                    backuptime=backuptime, backupfrequency=backupfrequencyvalue, weekday=weekdayvalue, monthday=monthdayvalue)

        #return Response({"result":"ok"})
        return Response(self.get_serializer(backupPolity).data, 200)

    def update(self, request, *args, **kwargs):

        name = request.DATA.get("name")
        backuptype = request.DATA.get("type", None)  #0-冷备份 , 1-热备份
        compress = request.DATA.get("compress", None)
        compresslevel = request.DATA.get("compresslevel", 0) # 0 ~ 9
        remain = request.DATA.get("remain", 0)  #0-所有 or 天数
        remaindays = request.DATA.get("remaindays", 0);
        locationtype = request.DATA.get("locationtype", None)  #0-本地 , 1-已关联存储 , 3-sftp
        localpath = request.DATA.get("localpath", None)
        storage_id = request.DATA.get("storage", None)
        sftpusername = request.DATA.get("sftpusername", None)
        sftphost = request.DATA.get("sftphost", None)
        sftppath = request.DATA.get("sftppath", None)
        sftppassword = request.DATA.get("sftppassword", None)
        backuptime = request.DATA.get("backuptime", None)
        backupfrequency = request.DATA.get("backupfrequency", None)
        weekday = request.DATA.get("weekday", None)
        monthday = request.DATA.get("monthday", None)

        backuptypevalue = None
        if backuptype:
            backuptypevalue = backuptype.get("value")

        storage = None
        if storage_id:
            storage = Storage.objects.get(id = storage_id.get("value"))

        backupfrequencyvalue = None
        if backupfrequency:
            backupfrequencyvalue = backupfrequency.get("value")

        if cmp(remain, "1") == 0:
            remain = remaindays

        weekdayvalue = None
        if weekday:
            weekdayvalue = weekday.get("name")

        monthdayvalue = None
        if monthday:
            monthdayvalue = monthday.get("value")

        backupPolity = BackupPolicy.objects.get_or_none(id=request.DATA.get('id'))
        backupPolity.update(dict(name=name, backuptype=backuptypevalue, compress=compress, compresslevel=compresslevel,
                                 remain=remain, locationtype=locationtype, localpath=localpath, storage=storage,
                                 sftpusername=sftpusername, sftphost=sftphost, sftppath=sftppath, sftppassword=sftppassword,
                                 backuptime=backuptime, backupfrequency=backupfrequencyvalue, weekday=weekdayvalue, monthday=monthdayvalue))

        #return Response({"result":"ok"})
        return Response(self.get_serializer(backupPolity).data, 200)

    def delete_all(self, request, *args, **kwargs):

        ids = request.DATA.get('ids')
        for id in ids:
            BackupPolicy.objects.get(id=id).remove()

        return self._get(request, args, kwargs)


class BackupstrategyDetail(BaseRetriveAPIView):
    serializer_class = BackupstrategySerializer
    model = BackupPolicy